package com.yc.dygang;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Demo1 {

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
        A a = new A();
        check(a);
    }

    public static void check(Object t) throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
        final Class<?> aClass = t.getClass();
        System.out.println("aClass.getName() = " + aClass.getName());
        System.out.println("aClass.getSimpleName() = " + aClass.getSimpleName());

        System.out.println("公共属性 = " + Arrays.toString(aClass.getFields()));
        System.out.println("定义的属性 = " + Arrays.toString(aClass.getDeclaredFields()));

        final Field name = aClass.getField("name");
        name.set( t, "张三");
        final Object o = name.get(t);
        System.out.println("o = " + o);
        final Field age = aClass.getDeclaredField("age");
        age.setAccessible(true);  // 设置强制访问
        age.set(t, 10);
        System.out.println("age = " + age.get(t));

        final Method eat = aClass.getDeclaredMethod("eat", String.class);
        eat.setAccessible(true);
        eat.invoke(t, "肯德基");

        aClass.getMethod("eat", String.class);

        // 练习题: 假设输入 B 对象, 请在控制台输出它的所有的属性, 包括父类的属性
        aClass.getSuperclass();//  父类

    }

}

class B extends A{
    String type;
    void sleep(){

    }
}

class A{
    private int age;
    public String name;

    public void play(){

    }

    private void eat(String food){
        System.out.println("food = " + food);
    }
}
